
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>View decorators &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="File Uploads" href="file-uploads.html" />
    <link rel="prev" title="Writing views" href="views.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="views.html" title="Writing views">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="file-uploads.html" title="File Uploads">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-http-decorators">
            
  <div class="section" id="s-module-django.views.decorators.http">
<span id="s-view-decorators"></span><span id="module-django.views.decorators.http"></span><span id="view-decorators"></span><h1>View decorators<a class="headerlink" href="#module-django.views.decorators.http" title="永久链接至标题">¶</a></h1>
<p>Django provides several decorators that can be applied to views to support
various HTTP features.</p>
<p>See <a class="reference internal" href="../class-based-views/intro.html#id1"><span class="std std-ref">Decorating the class</span></a> for how to use these decorators with
class-based views.</p>
<div class="section" id="s-allowed-http-methods">
<span id="allowed-http-methods"></span><h2>Allowed HTTP methods<a class="headerlink" href="#allowed-http-methods" title="永久链接至标题">¶</a></h2>
<p>The decorators in <a class="reference internal" href="#module-django.views.decorators.http" title="django.views.decorators.http"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.decorators.http</span></code></a> can be used to restrict
access to views based on the request method. These decorators will return
a <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponseNotAllowed" title="django.http.HttpResponseNotAllowed"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.http.HttpResponseNotAllowed</span></code></a> if the conditions are not met.</p>
<dl class="function">
<dt id="django.views.decorators.http.require_http_methods">
<code class="descname">require_http_methods</code>(<em>request_method_list</em>)<a class="headerlink" href="#django.views.decorators.http.require_http_methods" title="永久链接至目标">¶</a></dt>
<dd><p>Decorator to require that a view only accepts particular request
methods. Usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.decorators.http</span> <span class="kn">import</span> <span class="n">require_http_methods</span>

<span class="nd">@require_http_methods</span><span class="p">([</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;POST&quot;</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># I can assume now that only GET or POST requests make it this far</span>
    <span class="c1"># ...</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>Note that request methods should be in uppercase.</p>
</dd></dl>

<dl class="function">
<dt id="django.views.decorators.http.require_GET">
<code class="descname">require_GET</code>()<a class="headerlink" href="#django.views.decorators.http.require_GET" title="永久链接至目标">¶</a></dt>
<dd><p>Decorator to require that a view only accepts the GET method.</p>
</dd></dl>

<dl class="function">
<dt id="django.views.decorators.http.require_POST">
<code class="descname">require_POST</code>()<a class="headerlink" href="#django.views.decorators.http.require_POST" title="永久链接至目标">¶</a></dt>
<dd><p>Decorator to require that a view only accepts the POST method.</p>
</dd></dl>

<dl class="function">
<dt id="django.views.decorators.http.require_safe">
<code class="descname">require_safe</code>()<a class="headerlink" href="#django.views.decorators.http.require_safe" title="永久链接至目标">¶</a></dt>
<dd><p>Decorator to require that a view only accepts the GET and HEAD methods.
These methods are commonly considered &quot;safe&quot; because they should not have
the significance of taking an action other than retrieving the requested
resource.</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">Web servers should automatically strip the content of responses to HEAD
requests while leaving the headers unchanged, so you may handle HEAD
requests exactly like GET requests in your views. Since some software,
such as link checkers, rely on HEAD requests, you might prefer
using <code class="docutils literal notranslate"><span class="pre">require_safe</span></code> instead of <code class="docutils literal notranslate"><span class="pre">require_GET</span></code>.</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-conditional-view-processing">
<span id="conditional-view-processing"></span><h2>Conditional view processing<a class="headerlink" href="#conditional-view-processing" title="永久链接至标题">¶</a></h2>
<p>The following decorators in <a class="reference internal" href="#module-django.views.decorators.http" title="django.views.decorators.http"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.decorators.http</span></code></a> can be used to
control caching behavior on particular views.</p>
<dl class="function">
<dt id="django.views.decorators.http.condition">
<code class="descname">condition</code>(<em>etag_func=None</em>, <em>last_modified_func=None</em>)<a class="headerlink" href="#django.views.decorators.http.condition" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.views.decorators.http.etag">
<code class="descname">etag</code>(<em>etag_func</em>)<a class="headerlink" href="#django.views.decorators.http.etag" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.views.decorators.http.last_modified">
<code class="descname">last_modified</code>(<em>last_modified_func</em>)<a class="headerlink" href="#django.views.decorators.http.last_modified" title="永久链接至目标">¶</a></dt>
<dd><p>These decorators can be used to generate <code class="docutils literal notranslate"><span class="pre">ETag</span></code> and <code class="docutils literal notranslate"><span class="pre">Last-Modified</span></code>
headers; see
<a class="reference internal" href="../conditional-view-processing.html"><span class="doc">conditional view processing</span></a>.</p>
</dd></dl>

<span class="target" id="module-django.views.decorators.gzip"></span></div>
<div class="section" id="s-gzip-compression">
<span id="gzip-compression"></span><h2>GZip compression<a class="headerlink" href="#gzip-compression" title="永久链接至标题">¶</a></h2>
<p>The decorators in <a class="reference internal" href="#module-django.views.decorators.gzip" title="django.views.decorators.gzip"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.decorators.gzip</span></code></a> control content
compression on a per-view basis.</p>
<dl class="function">
<dt id="django.views.decorators.gzip.gzip_page">
<code class="descname">gzip_page</code>()<a class="headerlink" href="#django.views.decorators.gzip.gzip_page" title="永久链接至目标">¶</a></dt>
<dd><p>This decorator compresses content if the browser allows gzip compression.
It sets the <code class="docutils literal notranslate"><span class="pre">Vary</span></code> header accordingly, so that caches will base their
storage on the <code class="docutils literal notranslate"><span class="pre">Accept-Encoding</span></code> header.</p>
</dd></dl>

<span class="target" id="module-django.views.decorators.vary"></span></div>
<div class="section" id="s-vary-headers">
<span id="vary-headers"></span><h2>Vary headers<a class="headerlink" href="#vary-headers" title="永久链接至标题">¶</a></h2>
<p>The decorators in <a class="reference internal" href="#module-django.views.decorators.vary" title="django.views.decorators.vary"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.decorators.vary</span></code></a> can be used to control
caching based on specific request headers.</p>
<dl class="function">
<dt id="django.views.decorators.vary.vary_on_cookie">
<code class="descname">vary_on_cookie</code>(<em>func</em>)<a class="headerlink" href="#django.views.decorators.vary.vary_on_cookie" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="django.views.decorators.vary.vary_on_headers">
<code class="descname">vary_on_headers</code>(<em>*headers</em>)<a class="headerlink" href="#django.views.decorators.vary.vary_on_headers" title="永久链接至目标">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">Vary</span></code> header defines which request headers a cache mechanism should take
into account when building its cache key.</p>
<p>See <a class="reference internal" href="../cache.html#using-vary-headers"><span class="std std-ref">using vary headers</span></a>.</p>
</dd></dl>

<span class="target" id="module-django.views.decorators.cache"></span></div>
<div class="section" id="s-caching">
<span id="caching"></span><h2>Caching<a class="headerlink" href="#caching" title="永久链接至标题">¶</a></h2>
<p>The decorators in <a class="reference internal" href="#module-django.views.decorators.cache" title="django.views.decorators.cache"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.decorators.cache</span></code></a> control server and
client-side caching.</p>
<dl class="function">
<dt id="django.views.decorators.cache.cache_control">
<code class="descname">cache_control</code>(<em>**kwargs</em>)<a class="headerlink" href="#django.views.decorators.cache.cache_control" title="永久链接至目标">¶</a></dt>
<dd><p>This decorator patches the response's <code class="docutils literal notranslate"><span class="pre">Cache-Control</span></code> header by adding
all of the keyword arguments to it. See
<a class="reference internal" href="../../ref/utils.html#django.utils.cache.patch_cache_control" title="django.utils.cache.patch_cache_control"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_cache_control()</span></code></a> for the details of the
transformation.</p>
</dd></dl>

<dl class="function">
<dt id="django.views.decorators.cache.never_cache">
<code class="descname">never_cache</code>(<em>view_func</em>)<a class="headerlink" href="#django.views.decorators.cache.never_cache" title="永久链接至目标">¶</a></dt>
<dd><p>This decorator adds a <code class="docutils literal notranslate"><span class="pre">Cache-Control:</span> <span class="pre">max-age=0,</span> <span class="pre">no-cache,</span> <span class="pre">no-store,</span>
<span class="pre">must-revalidate,</span> <span class="pre">private</span></code> header to a response to indicate that a page
should never be cached.</p>
</dd></dl>

<span class="target" id="module-django.views.decorators.common"></span></div>
<div class="section" id="s-common">
<span id="common"></span><h2>Common<a class="headerlink" href="#common" title="永久链接至标题">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>The decorators in <a class="reference internal" href="#module-django.views.decorators.common" title="django.views.decorators.common"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.decorators.common</span></code></a> allow per-view
customization of <a class="reference internal" href="../../ref/middleware.html#django.middleware.common.CommonMiddleware" title="django.middleware.common.CommonMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">CommonMiddleware</span></code></a> behavior.</p>
<dl class="function">
<dt id="django.views.decorators.common.no_append_slash">
<code class="descname">no_append_slash</code>()<a class="headerlink" href="#django.views.decorators.common.no_append_slash" title="永久链接至目标">¶</a></dt>
<dd><p>This decorator allows individual views to be excluded from
<a class="reference internal" href="../../ref/settings.html#std:setting-APPEND_SLASH"><code class="xref std std-setting docutils literal notranslate"><span class="pre">APPEND_SLASH</span></code></a> URL normalization.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">View decorators</a><ul>
<li><a class="reference internal" href="#allowed-http-methods">Allowed HTTP methods</a></li>
<li><a class="reference internal" href="#conditional-view-processing">Conditional view processing</a></li>
<li><a class="reference internal" href="#gzip-compression">GZip compression</a></li>
<li><a class="reference internal" href="#vary-headers">Vary headers</a></li>
<li><a class="reference internal" href="#caching">Caching</a></li>
<li><a class="reference internal" href="#common">Common</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="views.html"
                        title="上一章">Writing views</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="file-uploads.html"
                        title="下一章">File Uploads</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/http/decorators.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="views.html" title="Writing views">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="file-uploads.html" title="File Uploads">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>